<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\JubaoModel;
use cmf\controller\AdminBaseController;

class AdminJubaoController extends AdminBaseController
{
    public function index() {
        $code = $this->request->get("report_code",'');
        $username = $this->request->get('username', '');
        $reportedName = $this->request->get('reported_name' , '');
        $startTime = $this->request->get('start_time' , '');
        $endTime = $this->request->get('end_time' , '');
        $jubaoModel = new JubaoModel();
        $select = $jubaoModel->field("*");
        if($code) {
            $select->where("report_code", '=', $code);
        }
        if($username) {
            $select->where("username", '=', $code);
        }
        if($reportedName) {
            $select->where("reported_name", '=', $reportedName);
        }
        if($startTime) {
            $select->where("created_at", ">=", $startTime);
        }
        if($endTime) {
            $select->where("created_at", "<=", $endTime);
        }
        $result = $select->order("id", "desc")->paginate(20);
        $this->assign("reportCode", $code);
        $this->assign("username", $username);
        $this->assign("reportedName", $reportedName);
        $this->assign("result", $result);
        $this->assign("startTime", $startTime);
        $this->assign("endTime", $endTime);
        return $this->fetch();
    }

    public function detail() {
        $id = $this->request->route('id','', 'intval');
        $jubaoModel = new JubaoModel();
        $data = $jubaoModel->find($id);
        if(!$data) {
            $this->error('举报不存在');
        }
        $data->attachlist = json_decode($data->attachlist, 1);
        $this->assign("data", $data);
        return $this->fetch();
    }

    public function reply() {
        $id = (int)$this->request->request("id");
        $content = $this->request->request('reply_content');
        if(!$content) {
            return $this->error('请输入回复内容');
        }
        $data = [
            'reply_content' => $content,
            'reply_time' => date("Y-m-d H:i:s")
        ];
        $jubaoModel = new JubaoModel();
        $res = $jubaoModel->save($data,['id' => $id]);
        if(!$res) {
            return $this->error('回复失败');
        }
        return $this->success('回复成功');
    }

    public function showindex() {
        $id = (int)$this->request->request('id');
        $model = new JubaoModel();
        $data = $model->find($id);
        if(!$data) {
            $this->error('不存在');
        }
//        $data = $data->toArray();
//        print_r($data);
//        $set['show_in_index'] = $data['show_in_index'] ? 0 : 1;
        $data->show_in_index = !$data->show_in_index;
        $data->save();
        return $this->success('修改成功');
    }

    public function delete() {
        $id = $this->request->param('id');
        $model = new JubaoModel();
        $data = $model->where("id", $id)->delete();
        return $this->success('删除成功', url('AdminJubao/index'));
    }
}
